trait Memento<T> {
    fn restore(self) -> T;
    fn print(&self);
}

struct MementoItem {
    state: String,
}

struct DataFrom {
    state: String,
}

impl DataFrom {
    pub fn save(&self) -> MementoItem {
        MementoItem {
            state: self.state.to_string(),
        }
    }
}

impl Memento<DataFrom> for MementoItem {
    fn restore(self) -> DataFrom {
        DataFrom {
            state: self.state.parse().unwrap(),
        }
    }

    fn print(&self) {
        println!("Originator backup: '{}'", self.state);
    }
}


#[test]
fn main() {
    let mut history = Vec::<MementoItem>::new();
    let mut originator = DataFrom { state: String::from("aa") };
    history.push(originator.save());
    originator.state = String::from("bb");
    history.push(originator.save());

    for moment in history.iter() {
        moment.print();
    }

    let originator = history.pop().unwrap().restore();
    println!("Restored to state: {}", originator.state);

    let originator = history.pop().unwrap().restore();
    println!("Restored to state: {}", originator.state);
}